package com.warrantyhotel.android.widget;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;

import com.warrantyhotel.android.R;

/**
 * Wrapper around AlertDialog for choosing edit image method
 * 
 */
public class TransferWarrantyDialog {

	private final AlertDialog dialog;

	
	/**
	 * 
	 * @param activity
	 * @param takeNewPhoto
	 * @param useCurrentImage
	 */
	
	public TransferWarrantyDialog(final Activity activity,
			final Runnable takeNewPhoto, final Runnable useCurrentImage) {
		dialog = makeDialog(activity, takeNewPhoto, useCurrentImage);
	}

	/**
	 * Creates AlertDialog with specific layout
	 * 
	 * @param activity
	 * @param takeNewPhoto
	 * @param useCurrentImage
	 * @return
	 */
	
	private AlertDialog makeDialog(final Activity activity,
			final Runnable takeNewPhoto, final Runnable useCurrentImage) {
		final AlertDialog.Builder builder = new Builder(activity);
		 builder.setIcon(R.drawable.icon);
		builder.setTitle(R.string.choose_variant);
		

		// layout
		final LayoutInflater inflater = (LayoutInflater) activity
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View layout = inflater.inflate(R.layout.edit_image_dialog,
				(ViewGroup) activity.findViewById(R.id.layout_root));
		builder.setView(layout);

		// cancel button
		builder.setNegativeButton(android.R.string.cancel, null);

		return setupDialog(builder.create(), layout, takeNewPhoto,
				useCurrentImage);
	}

	/**
	 * Setups buttons and actions to dialog
	 * 
	 * @param dialog
	 * @param layout
	 * @param takeNewPhoto
	 * @param useCurrentImage
	 * @return
	 */
	
	
	private AlertDialog setupDialog(final AlertDialog dialog,
			final View layout, final Runnable takeNewPhoto,
			final Runnable useCurrentImage) {
		final Button takeNewPhotoButton = (Button) layout
				.findViewById(R.id.take_new_photo_button);
		takeNewPhotoButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
//				DialogUtil.dismiss(dialog);
				dialog.dismiss();
				takeNewPhoto.run();
			}
		});

		final Button useCurrentImageButton = (Button) layout
				.findViewById(R.id.use_current_image_button);
		useCurrentImageButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(final View v) {
//				DialogUtil.dismiss(dialog);
				dialog.dismiss();
				useCurrentImage.run();
			}
		});



		return dialog;
	}

	/**
	 * Shows dialog
	 */
	public void show() {
		dialog.show();
	}
}
